package udpMVM;

import java.net.DatagramPacket;
import java.net.DatagramSocket;

public class Server {
    public static void main(String[] args) {
        DatagramSocket datagramSocket = null;
        try {
            System.out.println("----服务端启动----");
            //创建服务端对象 并赋予端口号
            datagramSocket = new DatagramSocket(8081);
            //先定义数组长度,并创建接收数据包,最大数据量为64kb
            byte[] bytes = new byte[1024 * 64];
            DatagramPacket datagramPacket = new DatagramPacket(bytes, bytes.length);
            while (true){
                System.out.println("----接受客户端数据----");

                //接受数据
                datagramSocket.receive(datagramPacket);

                //查看发送来的数据大小
                int length = datagramPacket.getLength();

                String s = new String(datagramPacket.getData(), 0, length);
                //打印数据，输出客户端ip，输出客户端端口
                System.out.println(s);
            }
//            System.out.println(datagramPacket.getAddress().getHostAddress());
//            System.out.println(datagramPacket.getAddress().getHostName());
//            System.out.println(datagramPacket.getPort());

        } catch (Exception e) {

        }finally {
            datagramSocket.close();
        }
    }
}
